import React, { Fragment } from 'react';
import { Empty, Statistic, Row, Col, Divider } from 'antd';
import ReactEcharts from '@/components/Echarts';
import styles from '../index.less';

const getPassOption = data => {
    // 公式：时间范围内转正成功的人数/时间范围内试用期人数*100%
    const seriesData = data.map(item => {
        if (!item.probationCount || !item.regularSuccessCount) {
            return 0;
        }
        return ((item.regularSuccessCount / item.probationCount) * 100).toFixed(2);
    });

    const option = {
        tooltip: {
            formatter(params) {
                let str = '';
                params.forEach(item => {
                    str += `${item.value} %`;
                });
                return str;
            },
            backgroundColor: '#485465',
            padding: [3, 9, 3, 9],
            textStyle: {
                fontSize: 12
            },
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: '11%',
            right: '3%',
            bottom: 40
        },
        xAxis: {
            type: 'category',
            data: data.map(item => item.yearMonth),
            axisLine: {
                lineStyle: {
                    color: '#D9D9D9'
                }
            },
            axisTick: {
                alignWithLabel: true,
                interval: 0
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.65)',
                interval: 0,
                formatter(value, index) {
                    if (index === seriesData.length - 1) {
                        return `{cur|${value}}`;
                    }
                    return value;
                },
                rich: {
                    cur: {
                        color: '#FFA22D',
                        fontSize: 12
                    }
                }
            }
        },
        yAxis: {
            type: 'value',
            name: '单位：%',
            nameTextStyle: {
                fontSize: 12,
                align: 'right',
                color: 'rgba(0, 0, 0, 0.65)',
                fontWeight: 500
            },
            // min: Math.min(...seriesData),
            // max: Math.max(...seriesData),
            splitLine: {
                lineStyle: {
                    color: '#E8E8E8',
                    type: 'dashed'
                }
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.65)',
                interval: 0,
                formatter(v) {
                    return v.toFixed(2);
                }
            }
        },
        series: [
            {
                data: seriesData,
                type: 'line',
                symbolSize: 6
            }
        ]
    };

    return option;
};

const getDelayOption = data => {
    // 公式：时间范围内转正延期且通过人数/转正延期人数*100%
    const seriesPassData = data.map(item => {
        if (!item.regularDelayCount || !item.delayButPassCount) {
            return 0;
        }
        return ((item.delayButPassCount / item.regularDelayCount) * 100).toFixed(2);
    });

    // 公式：时间范围内转正延期且离职人数/转正延期人数*100%
    const seriesLeaveData = data.map(item => {
        if (!item.regularDelayCount || !item.delayButLeaveCount) {
            return 0;
        }
        return ((item.delayButLeaveCount / item.regularDelayCount) * 100).toFixed(2);
    });
    const option = {
        tooltip: {
            formatter(params) {
                let str = `${params[0].axisValue}<br/>`;
                params.forEach(item => {
                    str += `<span style="color:rgba(255, 255, 255, 0.55);">${item.seriesName}：</span>${item.value} %<br/>`;
                });
                return str;
            },
            padding: [5, 9, 5, 9],
            axisPointer: {
                type: 'shadow'
            },
            backgroundColor: '#485465',
            textStyle: {
                fontSize: 12
            },
            trigger: 'axis'
        },
        legend: {
            data: ['转正延期且通过', '转正延期且离职'],
            bottom: 0,
            itemGap: 16,
            textStyle: {
                color: 'rgba(0, 0, 0, 0.65)'
            }
        },
        grid: {
            left: '11%',
            right: '3%',
            bottom: 70
        },
        xAxis: {
            type: 'category',
            data: data.map(item => item.yearMonth),
            axisLine: {
                lineStyle: {
                    color: '#D9D9D9'
                }
            },
            axisTick: {
                alignWithLabel: true,
                interval: 0
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.65)',
                interval: 0,
                formatter(value, index) {
                    if (index === seriesPassData.length - 1) {
                        return `{cur|${value}}`;
                    }
                    return value;
                },
                rich: {
                    cur: {
                        color: '#FFA22D',
                        fontSize: 12
                    }
                }
            }
        },
        yAxis: {
            type: 'value',
            name: '单位：%',
            nameTextStyle: {
                fontSize: 12,
                align: 'right',
                color: 'rgba(0, 0, 0, 0.65)',
                fontWeight: 500
            },
            interval: 20,
            // min: 0,
            // max: 100,
            splitLine: {
                lineStyle: {
                    color: '#E8E8E8',
                    type: 'dashed'
                }
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.65)'
            }
        },
        series: [
            {
                name: '转正延期且通过',
                type: 'bar',
                stack: '转正延期人数',
                data: seriesPassData,
                barCategoryGap: 6
            },
            {
                name: '转正延期且离职',
                type: 'bar',
                stack: '转正延期人数',
                data: seriesLeaveData,
                barCategoryGap: 6
            }
        ]
    };

    return option;
};

const Zzryfx = ({ passData, delayData }) => (
    <Fragment>
        <Row gutter={16}>
            <Col span={8}>
                <Statistic
                    title="新增申请转正人数："
                    value={passData.applyRegularCount ? passData.applyRegularCount : '0'}
                    suffix={passData.applyRegularCount ? '人' : ' '}
                />
            </Col>
            <Col span={8}>
                <Statistic
                    title="转正成功人数："
                    value={passData.regularSuccessCount ? passData.regularSuccessCount : '0'}
                    suffix={passData.regularSuccessCount ? '人' : ' '}
                />
            </Col>
        </Row>
        <div className={styles.chartTitle}>转正通过率</div>
        {!passData.children || passData.children.length === 0 ? (
            <Empty style={{ margin: '80px 0' }} image={Empty.PRESENTED_IMAGE_SIMPLE} />
        ) : (
            <ReactEcharts style={{ height: 330 }} option={getPassOption(passData.children)} />
        )}
        <Divider />
        <div className={styles.chartTitle}>
            转正延期人数：
            {delayData.regularDelayCount ? delayData.regularDelayCount : '0'}
        </div>
        {!delayData.children || delayData.children.length === 0 ? (
            <Empty style={{ margin: '80px 0' }} image={Empty.PRESENTED_IMAGE_SIMPLE} />
        ) : (
            <ReactEcharts style={{ height: 330 }} option={getDelayOption(delayData.children)} />
        )}
    </Fragment>
);

export default Zzryfx;
